
package kaizone.haining.chaobohui.fragment;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.widget.FirstReadNearbyCellView;
import kaizone.haining.chaobohui.widget.FirstReadNearbyView;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

public class FirstReadNearbyFragment extends LinearLayout {

    public static final int BOTTOM_SPACE = 100;

    private List<Item> mData;

    private MyAdapter mAdapter;

    private ListView mListView;

    private ImageFetcher mImageFetcher;

    public void setCultureList(List<Item> data) {
        mData = data;
    }

    public FirstReadNearbyFragment(Context context) {
        super(context);

    }

    public FirstReadNearbyFragment(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ImageView imageView = (ImageView) findViewById(R.id.img);
        mListView = (ListView) findViewById(R.id.listview);
        initListView();
    }

    private void initListView() {
        mAdapter = new MyAdapter();
        mListView.setAdapter(mAdapter);
        ViewTreeObserver observer = mListView.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ListAdapter adapter = mListView.getAdapter();
                View child = adapter.getView(0, null, mListView);
                if (child != null && mData != null) {
                    child.measure(0, 0);
                    int height = child.getMeasuredHeight();
                    int count = mData.size();
                    int totolHeight = height * count + BOTTOM_SPACE;
                    View parent = (View) mListView.getParent();
                    if (parent instanceof LinearLayout) {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT, totolHeight);
                        mListView.setLayoutParams(params);
                    }
                    // Log.e("onGlobalLayout", "totalheight ="+totolHeight);
                }
            }
        });
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mData != null ? mData.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            FirstReadNearbyCellView nearbyView = null;
            if (convertView == null) {
                nearbyView = (FirstReadNearbyCellView) View.inflate(getContext(),
                        R.layout.item_firstread_nearby, null);
                convertView = nearbyView;
            }
            nearbyView = (FirstReadNearbyCellView) convertView;
            int size = mData.size();
            if (position < size) {
                Item item = mData.get(position);
                nearbyView.setName(item.getName());
                nearbyView.setIntro(item.getIntro());
                PicModel picModel = item.getPicModel();
                String sum100x000 = picModel.sum100x000;
                // nearbyView.loadImage(mImageFetcher, sum100x000);
            }
            return nearbyView;
        }

    }

}
